// pages/personal/myAccount/withdrawal/withdrawal.js
var app = getApp()
var api = require('../../../../utils/api')
var apis = api.api
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		value: '',
		yemoney: '0.00', //余额
		txDate: [],
		withdrawTime: [], //提现限定的时间
		show: false,
		isOpen: false,
		userWallet: {}, //用户余额信息
		realName: '', //用户真实姓名
		account: '', //支付宝账号
		txData: [], //提现渠道数据
		money: '', //提现的金额
		radio: '1', //默认选择第一个
		txType: 'wechat', //默认提现渠道为微信
		txId: '', //渠道id
		zfb: false, //如果有已绑定zfb则为true
		wechat: false, //如果有已绑定wechat则为true
		isClose: false, //是否禁止点击提现
		isTx: false, //显示通知栏的内容
		todayDate: null, //今天是周几
		type: null, //用户的角色
	},
	openTip() {
		this.setData({
			show: true
		})
	},
	getMoney(e) {
		// 获取提现的金额
		this.setData({
			money: e.detail
		})
	},
	onChange(event) {
		this.setData({
			radio: event.detail,
		});
	},
	getAccount(e) {
		// 获取用户输入的支付宝账号
		this.setData({
			account: e.detail.value
		})
	},
	getName(e) {
		//获取用户输入的真实姓名
		this.setData({
			realName: e.detail.value
		})
	},
	showPopup() {
		if (this.data.isShow == false || this.data.isHide == false) {
			this.setData({
				isOpen: true
			})
		} else {
			wx.showToast({
				title: '清先选择提现途径',
				icon: 'none'
			})
		}
	},
	onClose() {
		this.setData({
			isOpen: false
		});
	},
	close() {
		this.setData({
			show: false
		})
	},
	closePop() {
		// 关闭弹窗
		this.setData({
			isShow: true,
			isHide: true,
		})
	},
	changeIcon(e) {
		var that = this;
		let url = apis.host + 'index/create_withdraw_channel';
		if (e.currentTarget.dataset.index == 0) {
			wx.showModal({
				title: '提示',
				content: '确定绑定此微信吗？',
				success: (res) => {
					if (res.confirm) {
						let data = {
							type: 'wechat',
							openid: wx.getStorageSync('session').openid
						}
						// 创建渠道
						//https://dageda.api.hjdz.ltd/index/create_withdraw_channel
						apis.request(url, data, 'POST').then(function(res) {
							if (res.code == 200) {
								wx.showToast({
									title: '绑定微信成功',
									icon: 'success'
								})
								that.setData({
									txData: that.data.txData,
									radio: '1',
									txType: 'wechat',
									show: false,
								})
							}
						}).catch(function(res) {})
					} else {
						wx.showToast({
							title: '你取消了选择',
							icon: 'none'
						})
					}
				}
			})
		} else if (e.currentTarget.dataset.index == 1) {
			// 创建渠道
			//https://dageda.api.hjdz.ltd/index/create_withdraw_channel
			that.setData({
				isOpen: true
			})
		} else {
			wx.showToast({
				title: '出现错误，请重试！',
				icon: 'none'
			})
		}
	},
	ok() {
		var that = this;
		let url = apis.host + 'index/create_withdraw_channel';
		let data = {
			type: 'zfb',
			realname: that.data.realName,
			account: that.data.account,
		}
		if (that.data.realName != '' && that.data.account != '') {
			apis.request(url, data, 'POST').then(function(res) {
				if (res.code == 200) {
					wx.showToast({
						title: '绑定支付宝成功',
						icon: 'success'
					})
					that.setData({
						isOpen: false,
						radio: '2',
						txType: 'zfb',
						show: false,
					})
				}
			}).catch(function(res) {
				wx.showToast({
					title: '绑定支付宝失败',
					icon: 'none'
				})
			})
		} else {
			wx.showToast({
				title: '有一项的值为空，请补充',
				icon: 'none'
			})
		}
	},
	getDate() {
		//判断今天是周几
		var that = this;
		var nowDate = new Date()
		var day = nowDate.getDay().toString();
		var today = nowDate.getDate().toString()
		var hour = nowDate.getHours();
		var dates = this.data.txDate;
		if (today === '20') {
			that.setData({
				isClose: false,
				isTx: true,
				todayDate: today
			})
		} else {
			that.setData({
				isClose: true,
				isTx: false,
				todayDate: 20
			})
		}
		// if (dates.indexOf(day) != -1 && hour >= 9 && hour < 24) {
		// 	//判断今天是否是可以提现的时间和日期，是的话按钮可以点击，否则禁用按钮
		// 	that.setData({
		// 		isClose: false,
		// 		isTx: true,
		// 	})
		// } else {
		// 	that.setData({
		// 		isClose: true,
		// 		isTx: false
		// 	})
		// }
		// var data = [{
		// 	'0': '星期日'
		// }, {
		// 	'1': '星期一'
		// }, {
		// 	'2': '星期二'
		// }, {
		// 	'3': '星期三'
		// }, {
		// 	'4': '星期四'
		// }, {
		// 	'5': '星期五'
		// }, {
		// 	'6': '星期六'
		// }];
		// //遍历获取数组里面的对象
		// for (var i = 0; i < data.length; i++) {
		// 	//console.log(data[i])
		// 	//再次遍历对象，将对象里面的value取出来
		// 	for (var x in data[i]) {
		// 		if (day == i) {
		// 			// console.log("今天是：" + data[i][x]);
		// 			that.setData({
		// 				todayDate: data[i][x]
		// 			})
		// 		} else {
		// 			// console.log("今天不是：" + data[i][x]);
		// 		}
		// 	}
		// }
	},
	goTxStory() {
		//去提现记录页面
		wx.navigateTo({
			url: '/pages/personal/myAccount/withdrawalHistory/withdrawalHistory'
		})
	},
	goProblem() {
		wx.navigateTo({
			url: '/pages/personal/myAccount/commonProblem/commonProblem'
		})
	},
	changetab(e) {
		var that = this;
		// 切换提现渠道
		const {
			name
		} = e.currentTarget.dataset;
		this.setData({
			radio: name,
		});
		if (e.currentTarget.dataset.name == '1') {
			that.setData({
				txType: 'wechat'
			})
		} else {
			that.setData({
				txType: 'zfb'
			})
		}
	},
	tx() {
		this.setData({
			show: false
		})
	},
	getNowTime() {
		//获取当前时间
		var date = new Date()
		var hour = date.getHours()
		return hour
	},
	checkAuditTime(beginTime, endTime) {
		var nowDate = new Date();
		var beginDate = new Date(nowDate);
		var endDate = new Date(nowDate);

		var beginIndex = beginTime.lastIndexOf("\:");
		var beginHour = beginTime.substring(0, beginIndex);
		var beginMinue = beginTime.substring(beginIndex + 1, beginTime.length);
		beginDate.setHours(beginHour, beginMinue, 0, 0);

		var endIndex = endTime.lastIndexOf("\:");
		var endHour = endTime.substring(0, endIndex);
		var endMinue = endTime.substring(endIndex + 1, endTime.length);
		endDate.setHours(endHour, endMinue, 0, 0);
		return nowDate.getTime() - beginDate.getTime() >= 0 && nowDate.getTime() <= endDate.getTime();
	},
	totx() {
		// 提现
		// https://dageda.api.hjdz.ltd/index/withdraw

		var that = this;
		if (that.data.money != '' && that.data.money) {
			wx.showModal({
				title: '提示',
				content: '由于春节高峰期，暂时不支持提现操作，具体时间请等通知'
			})
			return
			let url = apis.host + 'index/withdraw';
			let minTxMoney = wx.getStorageSync('userConfig').user.with_min
			if (Number(that.data.money) < Number(minTxMoney)) {
				wx.showModal({
					title: '提示',
					content: '您的金额未达到提现条件',
				})
			} else {
				let data = {
					price: that.data.money,
					/* 是 string 提现金额 */
					channel: that.data.txId,
					type: that.data.type,
					/* 是 string 提现渠道ID */
				};
				apis.request(url, data, 'POST').then(function(res) {
					if (res.code == 200) {
						var userInfo = wx.getStorageSync('userAllInfo')
						userInfo.wallet.map((item, index) => {
							if (item.type == that.data.type) {
								userInfo.wallet[index].commission = Number(item.commission * 1 -
									that.data.money * 1).toFixed(2)
							}
						})
						wx.setStorageSync('userAllInfo', userInfo)
						that.data.userWallet.commission = Number(that.data.userWallet.commission * 1 -
							that
							.data.money * 1).toFixed(2)
						wx.showToast({
							title: res.msg,
							icon: 'none'
						})
						that.setData({
							userWallet: that.data.userWallet,
							money: ""
						})
					}
				})
			}
		} else {
			wx.showToast({
				title: '请输入提现金额',
				icon: 'none'
			})
		}
	},
	getData() {
		let that = this;
		let txDate = wx.getStorageSync('userConfig').user.week_day
		let withdrawTime = wx.getStorageSync('userConfig').user.withdraw_time
		let txTime = withdrawTime.split('-')
		if (!this.checkAuditTime(txTime[0], txTime[1])) {
			wx.showModal({
				title: '提示',
				content: '当前时间不可提现',
				success: function(res) {
					if (res.confirm) {
						wx.navigateBack()
					} else {
						wx.navigateBack()
					}
				}
			})
		}
		that.setData({
			txDate: txDate,
			withdrawTime: withdrawTime
		})
		let url = apis.host + 'index/accountLog';
		let getWxUrl = apis.host + 'index/getWithdrawChannel';
		let getWxData = {};
		let data = {
			type: 0,
			/* 否	string	类型 默认0 全部 1支出 2收入 */
			page: 1,
			/* 否	string	页码 */
			size: 1 /* 是	string	页额 */
		};
		apis.request(url, data, 'POST').then(function(res) {}).catch(function(res) {})
		// https://dageda.api.hjdz.ltd/index/getWithdrawChannel
		apis.request(getWxUrl, getWxData, 'POST').then(function(res) {
			let data = res.data;
			data.map((value, index) => {
				if (value.type == 'zfb') {
					that.setData({
						txId: data[index].id,
						zfb: true
					})
				} else if (value.type == 'wechat') {
					that.setData({
						txId: data[index].id,
						wechat: true
					})
				}
			})
			that.setData({
				txData: res.data
			})
		}).catch(function(res) {})
	},
	txAll() {
		//提现所有
		this.setData({
			money: this.data.userWallet.commission
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var type = options.type;
		this.setData({
			type: type
		})
		// https://dageda.api.hjdz.ltd/index/accountLog
	},
	onShow() {
		var that = this;
		var userWallets = wx.getStorageSync('userAllInfo').wallet
		var userInfo = wx.getStorageSync('userAllInfo')
		userWallets.map((item, index) => {
			if (item.type == that.data.type) {
				that.data.userWallet = item
			}
		})
		if (JSON.stringify(that.data.userWallet) === '{}') {
			that.data.userWallet = {
				id: 0,
				user_id: userInfo.id,
				type: that.data.type,
				account: '0.00',
				score: 0,
				commission: '0.00'
			};
		}
		that.setData({
			userWallet: that.data.userWallet
		})
		that.getData()
		that.getDate()
	},
})
